package com.skateboard.reelviewtest.loopviewpager

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.skateboard.reelviewtest.R
import kotlinx.android.synthetic.main.activity_view_pager.*
import java.lang.Math.abs

class ViewPagerActivity : AppCompatActivity() {
    var adapter:MyPagerAdapter?=null
    var currentPosition = -1
    var currentPosition2=-1
    var deleteValue=false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager)
        var adapter = MyPagerAdapter(this)
        vp_test.adapter=adapter
        vp_test.addOnPageChangeListener(object:ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                adapter.dataProvider?.setCurrentAdapterPosition(position)
                currentPosition=position
                Log.e("ssss", "====================onPageSelected$position")
            }
        })
        vp_test.currentItem = DataProvider.initialDataPositionInViewPager

        btn_delete.setOnClickListener {
            deleteValue=true
        }

        vp_test.setPageTransformer(false) { page, position ->
            if(position==0f&&currentPosition!=currentPosition2){
                var value=adapter.dataProvider!!.getData(currentPosition)
                if(deleteValue){
                    while (kotlin.math.abs(value!!.toInt()) %2!=0){
                        adapter.dataProvider!!.deleteData(value,currentPosition2<currentPosition)
                        value=adapter.dataProvider!!.getData(currentPosition)
                    }
                }
                if(!value.isNullOrEmpty()){
                    var tv=page.findViewById<TextView>(currentPosition)
                    tv.text="AdapterPosition is :$currentPosition \n Content value:$value"
                }

                currentPosition2=currentPosition
            }
        }


    }


}